package com.yourname.www.frame.dialog;

import com.yourname.www.dao.JdbcHelper;
import com.yourname.www.entity.Student;
import com.yourname.www.model.StudentModel;
import com.yourname.www.util.Tools;
import com.yourname.www.util.WindowUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryMoreDialog extends JDialog {
    private JDialog jf;
    private Container c;
    private JLabel studentID;    //"学号"标签。
    private JLabel studentName;	//"姓名"标签。
    private JLabel sexLabel;	//"性别"标签。l
    private JLabel classeLabel;	//"班级"标签。
    private JLabel gradeLabel;	//"年级标签"。
    private JLabel majorLabel;	//"专业"标签。
    private JLabel departmentLabel;	//"所属院系"标签。
    private JTextField studentIDText;	//"学号"文本域。
    private JTextField studentNameText;	//"姓名"文本域。
    private JTextField sexText;	//性别选项
    private JTextField gradeText;	//年级选项
    private JTextField departmentText;	//系别
    private JTextField majorText;	//专业
    private JTextField classeText;	//班级
    private JButton conditionsButton;	//多条件查询按钮
    private JdbcHelper jdbcHelper;
    private JTable studentModel;
    public QueryMoreDialog(JDialog parent,JdbcHelper jdbcHelper,JTable studentTable){
        //参数1 父窗口
        //参数2 标题
        //参数3 true 表示当前对话框显示在最上层
        super(parent,"更多查询",true);
        setBasicInfo();
        this.setLayout(null);
        c = this.getContentPane();
        studentID = new JLabel("学号:");
        studentID.setBounds(29, 19, 30, 20);
        this.add(studentID);

        studentIDText = new JTextField();
        studentIDText.setBounds(65, 19, 100, 20);
        this.add(studentIDText);

        studentName = new JLabel("姓名:");
        studentName.setBounds(200, 19, 30, 20);
        this.add(studentName);

        studentNameText = new JTextField();
        studentNameText.setBounds(240, 19, 100, 20);
        this.add(studentNameText);

        sexLabel = new JLabel("性别:");
        sexLabel.setBounds(29, 50, 30, 20);
        this.add(sexLabel);

        sexText = new JTextField();
        sexText.setBounds(65, 50, 100, 20);
        this.add(sexText);

        gradeLabel = new JLabel("年级:");
        gradeLabel.setBounds(200, 50, 30, 20);
        this.add(gradeLabel);

        gradeText = new JTextField();
        gradeText.setBounds(240, 50, 100, 20);
        this.add(gradeText);

        departmentLabel = new JLabel("院系:");
        departmentLabel.setBounds(29, 83, 30, 20);
        this.add(departmentLabel);

        departmentText = new JTextField();
        departmentText.setBounds(65, 83, 100, 20);
        this.add(departmentText);

        majorLabel = new JLabel("专业:");
        majorLabel.setBounds(200, 83, 30, 20);
        this.add(majorLabel);

        majorText = new JTextField();
        majorText.setBounds(240, 83, 100, 20);
        this.add(majorText);

        classeLabel = new JLabel("班级:");
        classeLabel.setBounds(29,116, 30, 20);
        this.add(classeLabel);

        classeText = new JTextField();
        classeText.setBounds(65, 116, 100, 20);
        this.add(classeText);

        conditionsButton = new JButton("多条件查询");
        conditionsButton.setBounds(230, 130, 100, 30);
        this.add(conditionsButton);
        conditionsButton.addActionListener(new ActionListener() {

            private  Map<String,String> where = new HashMap<>();
            @Override
            public void actionPerformed(ActionEvent e) {
                setWhere(studentIDText,"Student_Id");
                setWhere(studentNameText,"Student_Name");
                setWhere(sexText,"Student_sex");
                setWhere(majorText,"Major_Name");
                setWhere(departmentText,"Department_Name");
                setWhere(studentIDText,"Student_Id");
                setWhere(gradeText,"Grade");
                setWhere(classeText,"Classe");
                List<Student> studentList = jdbcHelper.queryStudentList(where);
                //重置表格数据
                studentTable.setModel(new StudentModel(studentList));
                //自适应表格宽度
                Tools.setTableColWidth(studentTable,new int[]{80,80,40,60,30,160,160});
                jf.dispose();

            }
            public void setWhere(JTextField jTextField,String key){
                String s = jTextField.getText().trim();
                if(s.length()>0){
                    this.where.put(key,s);
                }
            }
        });

        this.setSize(411, 222);
        this.setVisible(true);
    }
    //设置基本信息
    private void setBasicInfo() {
        this.jf = this;
        this.setTitle("多条件查询");
        //设置Logo
        try{
            Image image = ImageIO.read(this.getClass().getResource("/logo.png"));
            this.setIconImage(image);
        }catch (IOException e) {
            e.printStackTrace();
        }
        //设置大小
        this.setSize(400,300);
        //设置是否可以调节大小
        this.setResizable(false);
        //设置关闭行为【其实默认就是，可以不设置，了解方法即可】
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置居中
        WindowUtil.setFrameCenter(this);
    }
}
